<template>
  <div>
    <h3 style="text-align: center; margin-bottom: 1rem">个人信息</h3>
    <el-row>
      <el-col :span="8">
        <div class="avatar">
          <!-- <Avatar size="large"></Avatar> -->
          <h3>头像</h3>

          <el-upload
            class="avatar-uploader"
            action="/api/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.avatar" :src="`/api/${form.avatar}`" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
          <el-text type="primary">点击修改头像</el-text>
        </div>
      </el-col>
      <el-col :span="16">
        <el-form :model="form" label-width="120px">
          <el-form-item label="账号">
            <el-input v-model="form.userName" disabled />
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickName" />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" />
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.phone" />
          </el-form-item>
          <el-form-item label="生日">
            <el-date-picker
              v-model="form.birthday"
              type="date"
              placeholder="选择日期"
              style="width: 100%"
              @change="handleChange"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="Remark">
            <el-input v-model="form.remark" type="textarea" :rows="5" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import Avatar from "@/components/Avatar.vue";
import { Plus } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

import type { UploadProps } from "element-plus";

import { useUserStore } from "@/stores/user";
import { updateUser } from "@/api/user";

const imageUrl = ref("");

// do not use same name with ref
const form = ref<any>({
  nickName: "",
  userName: "",
  email: "",
  phone: "",
  birthday: "",
  remark: "",
  avatar: "",
});

const userStore = useUserStore();

function loadData() {
  userStore.getInfo().then((res: any) => {
    console.log(res);

    const { avatar, nickName, userName, phone, email, birthday, remark } = res.data;
    // console.error(avatar);

    form.value = {
      nickName,
      userName,
      phone,
      email,
      birthday,
      remark,
      avatar,
    };
  });
}

loadData();

function handleChange(val: Date) {
  console.log(val);
  console.log(form.value.birthday);
}

const onSubmit = () => {
  console.log("submit!");
  console.log(form.value);

  updateUser(form.value).then((res) => {
    if (res.code !== 200) {
      return ElMessage.error("保存失败");
    }
    ElMessage.success(res.message);
    userStore.getInfo();
  });
};

const handleAvatarSuccess: UploadProps["onSuccess"] = (response, uploadFile) => {
  console.log(response, uploadFile);

  imageUrl.value = URL.createObjectURL(uploadFile.raw!);
  form.value.avatar = response.data;
  console.log(form.value.avatar);
};

const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  const types = ["image/jpeg", "image/png", "image/gif"];
  if (!types.includes(rawFile.type)) {
    ElMessage.error(`支持上传${types.join(", ")}格式的图片`);
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("Avatar picture size can not exceed 2MB!");
    return false;
  }
  return true;
};
</script>

<style scoped lang="less">
.avatar {
  display: flex;
  flex-direction: column;
  align-items: center;
  & > * {
    margin: 0.5rem 0;
  }
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
